package com.study.services;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.mdao.LogforcustDAO;
import com.study.model.mdao.RepairGoodsMapper;
import com.study.model.mdao.UserMDAO;
import com.study.model.mdao.WeixiuTableMapper;

@Service
@Transactional(rollbackOn = Exception.class)
public class WeixiuTableService {
	@Autowired
	WeixiuTableMapper mapper;
	@Autowired
	RepairGoodsMapper rGoodsMapper;
	@Autowired
	UserMDAO userMDAO;
	@Autowired
	LogforcustDAO logforcustDAO;


	/**
	 * 查询当天的维修工单
	 * 
	 * @param string
	 * @return
	 */
	public List<JSONObject> queryByDay(String string) {

		return mapper.queryByDay(string);

	}

	/**
	 * 查询维修工单+分页+条件
	 * 
	 * @param date
	 * @param userid 
	 * @param i
	 * @param j
	 * @return
	 */
	public List<JSONObject> queryAll(Date date, Integer type, int currentPage, int pageSie, String userid) {
		
		SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd");
		String date1 = null;
		if (date != null) {
			date1 = format.format(date);
		}
		
		if (460 == type) {
			type = null;
		}
		String selectByBoss = userMDAO.selectByBoss(Integer.parseInt(userid));
		if (selectByBoss != null && selectByBoss.length() > 0) {
			userid = null;
		}
		PageHelper.startPage(currentPage, pageSie);
		return mapper.selectByWhere(date1, type,userid);
	}

	/**
	 * 查询维修详情
	 */
	public JSONObject queryById(String id) {
		JSONObject object = mapper.queryById(id);
		return object;
	}

	/**
	 * 修改维修工单的状态
	 * 
	 * @param id
	 * @param state
	 * @param sum 
	 * @param date 
	 * @param cusid 
	 * @return
	 */
	public boolean changeState(String id, Integer state, Date date, String sum,Integer useid, Integer cusid) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String date1 = null;
		if (date != null) {
			 date1 = format.format(date);
		}
		
		int row = mapper.changeState(id, state+"",date1,sum);
		
		//添加流水信息
		String logMessage = null;
		if (state == 465) {
			logMessage = "维修&nbsp;&nbsp;"+"维修人员开始维修......" +"====";
		}else if (state == 461) {
			logMessage = "维修&nbsp;&nbsp;"+"产品维修中......" +"====";
		}else if (state == 462) {
			logMessage = "维修&nbsp;&nbsp;"+"维系完成......" +"====";
		}
		 
		logforcustDAO.insert(cusid, new Date(), useid, 2202, logMessage,"");
				
		return row > 0?true:false;

	}

	/**
	 * 保存维修配件
	 * 
	 * @param repId
	 * @param attribute
	 * @return
	 */
	public boolean addPeiJian(String repId, JSONArray goods) {
		// JSONArray goods = (JSONArray) JSONArray.parse(attribute);
		int row = 0;
		boolean containsKey = goods.getJSONObject(0).containsKey("sum");
		if (containsKey) {
			goods.remove(0);
			for (Object object : goods) {
				JSONObject good = (JSONObject) object;
				String goodId = good.getString("id");
				String name = good.getString("name");
				String price = good.getString("price");
				String number = good.getString("number");
				String goodTotal = good.getString("goodTotal");
				row = rGoodsMapper.addPeiJian(repId, goodId, name, price, number, goodTotal);
			}
		}

		if (row > 0) {
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 添加完成维修的时间
	 * 
	 * @param id
	 * @param date
	 * @return
	 */
	public boolean changeFinishTime(String id, Date date) {
		int row = mapper.changeFinishTime(id, date);

		if (row > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 查询配件单
	 * 
	 * @param id
	 * @return
	 */
	public JSONArray getPeiJianCart(String id) {
		JSONArray goods = new JSONArray();

		List<JSONObject> list = rGoodsMapper.getPeiJianCart(id);
		int sum = 0;
		int number = 0;

		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				JSONObject object = new JSONObject();
				object.put("id", list.get(i).getString("goo_id"));
				object.put("name", list.get(i).getString("goo_name"));
				object.put("price", list.get(i).getInteger("goo_price"));
				object.put("number", list.get(i).getInteger("goo_number"));
				object.put("goodTotal", list.get(i).getInteger("goo_goodTotal"));
				goods.add(object);
				sum += list.get(i).getInteger("goo_goodTotal");
				number += list.get(i).getInteger("goo_number");
			}
		}
		JSONObject object = new JSONObject();
		object.put("sum", sum);
		object.put("number", number);
		goods.add(0, object);// 添加总金额+总数量
		return goods;
	}
	
	/**
	 * 添加评级
	 * @param id
	 * @param rate//等级
	 * @param remark//评论
	 * @return
	 */
	public boolean changeRate(String id, String rate, String remark) {
		int row = mapper.changeRate(id,rate,remark);
		return row>0?true:false;
	}

}
